package info.brenstar.boxedin.sprites;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Point;
import org.newdawn.slick.geom.Rectangle;

import info.brenstar.boxedin.core.*;

public class Box extends GravitySprite implements Updated {
	
	private int level, lastLevel;
	private boolean grounded = false;
	
	public Box(float x, float y) {
		super(x, y);
		this.mask = new Rectangle(x,y,32f,32f);
		this.collide = true;
		this.gravity_direction = 270;
		this.delta_gravity = 0.5;
		this.gravity_max = 12.0;
	}
	
	@Override
	public void update() {
		level = (int)Math.floor(y / 32.0);
		
		if (level != lastLevel && !grounded) {
			Rectangle rect = new Rectangle(x, y+33, 32f, (float)gravity);
			Sprite sprite = room.collidesWith(rect);
			if (sprite != null) {
				y = sprite.getY() - 32;
				grounded = true;
			}
		}
		
		if (!grounded) {
			applyGravity();
		}
		
		lastLevel = level;

	}

	@Override
	public void render(Graphics g) {
		g.fillRect(x, y, 32f, 32f);

	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	@Override
	public void unload() {
		// TODO Auto-generated method stub

	}

	@Override
	public void kill() {
		// TODO Auto-generated method stub

	}

}
